<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title=" showTitle" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="submit">确定</el-button>
          </div>
        </template>
      </el-dialog>
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addDialog(1,0)">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- 作用域插槽拿到当前数据  v-if判断 type为2的时候隐藏按钮 -->
              <el-button v-if="row.type ===1" size="small" type="text" @click="addDialog(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="clickShowEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="clickDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      list: [],
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    showTitle() {
      if (!this.formData.id) {
        return '添加权限'
      } else {
        return '查看编辑'
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 查看回显数据
    async clickShowEdit(id) {
      // 获取数据回显
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.showDialog = true
    },
    // 删除按钮
    async clickDel(id) {
      this.$confirm('确定要删除该权限吗?', '温馨提示').then(async() => {
        await reqDelPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      }).catch(() => {
        console.log('取消')
      })
    },
    // 点击确认按钮
    async submit() {
      // 非空校验 和 去除收尾空格
      if (!this.formData.name.trim()) {
        return this.$message.error('请输入权限名称')
      }
      if (!this.formData.code.trim()) {
        return this.$message.error('请输入权限标识')
      }
      // 发起请求
      if (this.formData.id) {
        // 编辑操作
        await reqUpdatePermission(this.formData)
      } else {
        // 添加操作
        await reqAddPermission(this.formData)
      }
      // 提示信息
      this.$message.success('操作成功')
      // 重新渲染
      this.getPermissionList()
      // 关闭弹窗
      this.closeDialog()
    },
    // 关闭弹窗方法
    closeDialog() {
      this.showDialog = false
    },
    // 点击打开权限弹窗
    addDialog(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // console.log('889', data)
      // console.log('888', JSON.parse(JSON.stringify(data)))
      this.list = tranListToTreeData(data, '0')
    }
  }
}
</script>
